import 'package:flutter/material.dart';
import 'package:flutter_rider/api/api.dart';
import 'package:flutter_rider/main/page/splash_page.dart';
import 'package:flutter_rider/main/widgets/dialog/alert_dialog.dart';
import 'package:flutter_rider/main/widgets/ext/u_button.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/ext/u_title_layout.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import '../../../utils/tip.dart';

class DebugPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return DebugPageState();
  }
}

class DebugPageState extends State<DebugPage> {
  final _hostTextController = TextEditingController();
  final _proxyTextController = TextEditingController();

  @override
  void dispose() {
    super.dispose();
    _hostTextController.dispose();
    _proxyTextController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: UIColor.color_main,
      appBar: UTitleLayout.getBackTitle(context, "debug"),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              4.hspace,
              12.wspace,
              Row(
                children: [

                  UText.getSubText("当前HOST为: "),
                  const Spacer(),
                  UText.getSubText(Api.HOST),
                ],
              ).padding(EdgeInsets.symmetric(horizontal: 10.rpx)),
              _renderHost(),
              Row(
                children: [
                  10.wspace,
                  UText.getBkButton("测试环境", 13.rpx,
                      width: 64.rpx,
                      height: 32.rpx,
                      radius: 4.rpx,
                      bkColor: UIColor.color_primary, onPressed: () {
                    _hostTextController.text = EnvHost.TEST;
                    _hostTextController.selection = TextSelection.fromPosition(
                        TextPosition(offset: _hostTextController.text.length));
                    setState(() {});
                  }),
                  10.wspace,
                  UText.getBkButton("预发布环境", 13.rpx,
                      width: 74.rpx,
                      height: 32.rpx,
                      radius: 4.rpx,
                      bkColor: UIColor.color_primary, onPressed: () {
                    _hostTextController.text = EnvHost.PRE;
                    _hostTextController.selection = TextSelection.fromPosition(
                        TextPosition(offset: _hostTextController.text.length));
                    setState(() {});
                  }),
                  10.wspace,
                  UText.getBkButton("正式环境", 13.rpx,
                      width: 74.rpx,
                      height: 32.rpx,
                      radius: 4.rpx,
                      bkColor: UIColor.color_primary, onPressed: () {
                    _hostTextController.text = EnvHost.RELEASE;
                    _hostTextController.selection = TextSelection.fromPosition(
                        TextPosition(offset: _hostTextController.text.length));
                    setState(() {});
                  }),
                ],
              ),
              UButton.getRoundRectButton("设置HOST",
                  width: double.infinity,
                  height: 40.rpx,
                  backgroundColor: UIColor.color_info, onPressed: () {
                // Api.getx(context, _hostTextController.text + "/v1/app/app/new_version",
                //     content: "确定切换host为: ${_hostTextController.text}",
                //     success: (data) {
                //   Api.setHost(_hostTextController.text);
                //   Api.logout();
                //   Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                //     builder: (context) {
                //       return SplashPage();
                //     },
                //   ), (route) => false);
                // }, successTip: "切换成功", failTip: "切换失败");
                  Api.setHost(_hostTextController.text);
                  Api.logout();
                  Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                    builder: (context) {
                      return SplashPage();
                    },
                  ), (route) => false);
                  Tip.show("切换成功");
              }),
              20.hspace,
              Row(
                children: [
                  UText.getSubText("当前代理为: "),
                  const Spacer(),
                  UText.getSubText(Api.PROXY),
                ],
              ).padding(EdgeInsets.symmetric(horizontal: 10.rpx)),
              _renderProxy(),
              10.hspace,
              UButton.getRoundRectButton("设置代理",
                  width: double.infinity,
                  height: 40.rpx,
                  backgroundColor: UIColor.color_info, onPressed: () {
                CustomAlertDialog.show(context,
                    content: "确定设置代理: ${_proxyTextController.text}",
                    onConfirm: () {
                  Api.setProxy(_proxyTextController.text);
                  Tip.show("代理设置生效");
                  setState(() {});
                });
              }),
            ],
          ),
        ),
      ),
    );
  }

  Widget _renderHost() {
    return TextField(
      style: TextStyle(color: UIColor.color_t1, fontSize: 16.rpx),
      controller: _hostTextController,
      cursorColor: UIColor.color_primary,
      cursorHeight: 20.rpx,
      keyboardType: TextInputType.text,
      maxLength: 40,
      decoration: InputDecoration(
        counterText: "",
        contentPadding:
            EdgeInsets.symmetric(horizontal: 20.rpx, vertical: 5.rpx),
        //prefixIconConstraints: BoxConstraints(minWidth: 32.rpx, minHeight: 24.rpx),
        //prefixIcon: SvgPicture.asset(R.image_login_user, width: 24.rpx, height: 24.rpx),
        border: UnderlineInputBorder(
          borderSide: BorderSide(color: UIColor.color_divider, width: 1.rpx),
        ),
        focusedBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: UIColor.color_primary, width: 1.rpx),
        ),
        labelText: "请输入host, 比如https://xxxxx",
        labelStyle:
            TextStyle(color: Colors.black.withOpacity(0.26), fontSize: 16.rpx),
        suffixIcon: _hostTextController.text.isNotEmpty
            ? IconButton(
                iconSize: 20.rpx,
                icon: Icon(
                  Icons.close,
                  color: UIColor.color_t3,
                  size: 20.rpx,
                ),
                onPressed: () {
                  _hostTextController.text = "";
                  setState(() {});
                },
              )
            : null,
      ),
      onChanged: (text) {
        setState(() {});
      },
    );
  }

  Widget _renderProxy() {
    return TextField(
      style: TextStyle(color: UIColor.color_t1, fontSize: 16.rpx),
      controller: _proxyTextController,
      cursorColor: UIColor.color_primary,
      cursorHeight: 20.rpx,
      keyboardType: TextInputType.text,
      maxLength: 40,
      decoration: InputDecoration(
        counterText: "",
        contentPadding:
            EdgeInsets.symmetric(horizontal: 20.rpx, vertical: 5.rpx),
        //prefixIconConstraints: BoxConstraints(minWidth: 32.rpx, minHeight: 24.rpx),
        //prefixIcon: SvgPicture.asset(R.image_login_user, width: 24.rpx, height: 24.rpx),
        border: UnderlineInputBorder(
          borderSide: BorderSide(color: UIColor.color_divider, width: 1.rpx),
        ),
        focusedBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: UIColor.color_primary, width: 1.rpx),
        ),
        labelText: "请输入proxy, 比如192.168.1.1:8888",
        labelStyle:
            TextStyle(color: Colors.black.withOpacity(0.26), fontSize: 16.rpx),
        suffixIcon: _proxyTextController.text.isNotEmpty
            ? IconButton(
                iconSize: 20.rpx,
                icon: Icon(
                  Icons.close,
                  color: UIColor.color_t3,
                  size: 20.rpx,
                ),
                onPressed: () {
                  _proxyTextController.text = "";
                  setState(() {});
                },
              )
            : null,
      ),
      onChanged: (text) {
        setState(() {});
      },
    );
  }
}
